package edu.uc.ui;

import edu.uc.dto.User;
import edu.uc.service.IUserService;
import edu.uc.service.UserServiceStub;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class CreateNewUserScreen extends CeasBaseScreen {
	private Spinner spinner;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create_new_user_screen);
		
		spinner = (Spinner) findViewById(R.id.spinPrefix);
		System.out.println("Spinner: " + spinner);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.user_prefix, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.create_new_user_screen, menu);
		return true;
	}
	
	public void onClick(View view) {
		Intent intent = null;
		switch (view.getId()) {
		case R.id.btnRegister:
			final String username = ((EditText) findViewById(R.id.txtUsername)).getText()
				.toString();
			final String pass = ((EditText) findViewById(R.id.txtPassword)).getText()
					.toString();
			final String confirmPwd = ((EditText) findViewById(R.id.confirmPwd)).getText()
					.toString();
			final String prefix = spinner.getSelectedItem().toString();
			final String firstName = ((EditText) findViewById(R.id.txtFirstName)).getText()
					.toString();
			final String lastName = ((EditText) findViewById(R.id.txtLastName)).getText()
					.toString();
			final String phoneNumber = ((EditText) findViewById(R.id.txtPhoneNumber)).getText()
					.toString();
			if (username == null || username.equals("")){
				Toast.makeText(CreateNewUserScreen.this, "User name is required",
						3).show();
				return;
			}
			if (pass == null || pass.equals("")){
				Toast.makeText(CreateNewUserScreen.this, "Password is required",
						3).show();
				return;
			}
			if (!pass.equals(confirmPwd)){
				Toast.makeText(CreateNewUserScreen.this, "Passwords do not match",
						3).show();
				return;
			}
			if (firstName == null || firstName.equals("")){
				Toast.makeText(CreateNewUserScreen.this, "First name is required",
						3).show();
				return;
			}
			if (lastName == null || lastName.equals("")){
				Toast.makeText(CreateNewUserScreen.this, "Last name is required",
						3).show();
				return;
			}
			if (phoneNumber == null || phoneNumber.equals("")){
				Toast.makeText(CreateNewUserScreen.this, "Phone Number is required",
						3).show();
				return;
			}
			
			IUserService service = new UserServiceStub();
			User user = new User();
			user.setUsername(username);
			user.setPassword(pass);
			user.setFirstName(firstName);
			user.setLastName(lastName);
			user.setPrefix(prefix);
			user.setPhoneNumber(phoneNumber);
			try {
				service.save(user);
			} catch (Exception e) {
				Toast.makeText(CreateNewUserScreen.this, "Error in saving user. Please contact administrator!",
						3).show();
				return;
			}
			
			Toast.makeText(CreateNewUserScreen.this, "User has been saved successfully",
					5).show();
			
			intent = new Intent(CreateNewUserScreen.this,
					IntroScreen.class);
			startActivity(intent);
			break;
		case R.id.btnMoveLogin:
			intent = new Intent(CreateNewUserScreen.this,
					IntroScreen.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}
}
